
//app.js

var api = require('api.js');
var util = require('utils/util.js');

App({
  userLogin:function (){
    var that = this;
    if (wx.getStorageSync("loginOk")) return ;//判断用户是否登入；避免频繁的请求服务器
    wx.login({
      success: function (res) {
        console.log(res);
        if (res.code) {
          //发起网络请求
          wx.request({
            url: api.userLogin,
            data: {
              code: res.code
            },
            success: function (res) {
              wx.setStorageSync("PHPSESSID", res.data.PHPSESSID);//保存用户的session_id
              that.globalData.session_key = res.data.session_key;
              that.globalData.openid = res.data.openid;
              that.globalData.isBindPhone = res.data.isbBindPhone;
              wx.setStorage({
                key: "openid",
                data: res.data.openid
              })
              console.log(res.data);
              //保存用户信息到 globalData.userInfo 用户变量
              //that.globalData.userInfo = data.data
              if (!!res.data.isBindPhone === false){
                wx.redirectTo({
                  url: '/pages/tel/tel'
                })
              }else{
                wx.setStorageSync("loginOk", true);//可正常访问状态
              }
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      },
      fail: function (res) {
        console.log("login fail");
      },
      complete: function () {
        console.log("complete");
      }
    });
  },
  onLaunch: function () {
    var that = this;
    that.userLogin() //重新登录
  },
  globalData: {
    session_key:null,
    openid:null,    
    userInfo: null,
    isPopping: false,//底部菜单是否已经弹出，
    isBindPhone:false,//是否已经绑定手机号
  },
  //点击弹出  
  plus: function () {
    if (this.data.isPopping) {
      //缩回动画  
      this.popp();
      this.setData({
        isPopping: false
      })
    } else if (!this.data.isPopping) {
      //弹出动画  
      this.takeback();
      this.setData({
        isPopping: true
      })
    }
  }, 
  popp: function () {
    //plus顺时针旋转  
    var animationPlus = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    }) 
  }
})